<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot-scope="{ row }" slot="menu">
        <el-button type="text" icon="el-icon-edit" size="small" v-if="permission.cloudStationTemplateManage_edit" @click.stop="handleEdit(row)">编辑 </el-button>
        <el-button type="text" size="small" icon="el-icon-delete" v-if="permission.cloudStationTemplateManage_delete" @click="rowDel(row)">删 除 </el-button>
      </template>
      <template slot="menuLeft">
        <el-button type="primary" size="small" icon="el-icon-plus" v-if="permission.cloudStationTemplateManage_add" @click="addBtn">新增</el-button>
        <el-button type="danger" size="small" icon="el-icon-delete" plain v-if="permission.cloudStationTemplateManage_delete" @click="handleDelete">删 除 </el-button>
      </template>
      <!--      <template slot="menuRight">-->
      <!--        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>-->
      <!--      </template>-->
    </avue-crud>
  </basic-container>
</template>

<script>
import { getWorkStationDetail, getStationtTemplateList, removeStationtTemplateInfo } from '@/api/product/station-manage';
import { mapGetters } from 'vuex';
import userTableConfig from '@/components/user-table-config';
import userConfig from '@/mixins/userConfig';
export default {
  name: 'CloudCloudStationTemplateManage',
  components: {
    userTableConfig
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'CLOUD_STATION_TEMPLATE_MANAGE',
      form: {},
      query: {},
      timer: new Date().getTime(),
      addDialog: false,
      editDialog: false,
      loading: false,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'calc(100vh - 360px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        indexLabel: '序号',
        addBtn: false,
        editBtn: false,
        delBtn: false,
        selection: true,
        dialogClickModal: false,
        columnBtn: false,
        column: [
          {
            label: '名称',
            prop: 'name',
            search: true
          },
          {
            label: '工站类型',
            prop: 'type',
            type: 'select',
            search: true,
            dicData: [
              //可做返回类型的转换
              {
                label: '过站',
                value: 'OVERSTATION'
              },
              {
                label: '报工',
                value: 'TIMESHEET'
              },
              {
                label: '测试',
                value: 'TEST'
              },
              {
                label: '老化',
                value: 'AGING'
              },
              {
                label: '包装',
                value: 'PACKING'
              },
              {
                label: '组装',
                value: 'ASSEMBLY'
              },
              {
                label: '上料',
                value: 'FEEDING'
              }
            ]
          },
          {
            label: '编码',
            prop: 'code',
            width: 250
          },
          {
            label: '客户端',
            prop: 'clientType',
            type: 'select',
            search: true,
            dicData: [
              {
                label: 'WEB',
                value: 'WEB'
              },
              {
                label: 'PDA',
                value: 'PDA'
              }
            ]
          },
          {
            label: '备注',
            prop: 'remark'
          },
          {
            label: '创建时间',
            prop: 'createTime'
          },
          {
            label: '最后更改时间',
            prop: 'updateTime'
          }
        ]
      },
      data: [],
      properties: {}, //表单
      title: ''
    };
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.cloudStationTemplateManage_add, false),
        delBtn: this.vaildData(this.permission.cloudStationTemplateManage_delete, false),
        editBtn: this.vaildData(this.permission.cloudStationTemplateManage_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  methods: {
    /**
     * 表单重置
     */
    emptytChange() {},

    reset() {
      this.addDialog = false;
      this.$refs.addProperties.resetForm();
    },
    /**
     * 跳转工站UI界面设计进行新增
     */
    addBtn() {
      this.title = '新增';
      // this.addDialog = true;
      this.$router.replace({ path: '/product/model-manage/stationui-design/index', query: { type: 'stationTemplate' } });
    },
    /**
     * 跳转工站UI界面设计进行编辑
     */
    handleEdit(row) {
      let stationId = this._get(row, 'id', '');
      this.$router.replace({ path: '/product/model-manage/stationui-design/edit', query: { type: 'stationTemplate', stationId: stationId } });
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeStationtTemplateInfo(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    },
    handleDelete(row) {
      if (this.selectionList.length === 0 && !row.id) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeStationtTemplateInfo(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.$refs.crud.toggleSelection();
        });
    },
    /**
     * 打开avue-crud之前的调用
     */
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
        getWorkStationDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getStationtTemplateList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    /**
     * 重新加载公用表格弹窗组件
     */
    newKey() {
      this.timer = new Date().getTime();
    }
  }
};
</script>

<style></style>
